﻿
namespace QSHC.QSchedule.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using QSHC.QSchedule.Web.Models;


    // Implements application logic using the QScheduleEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class QScheduleDomainService : LinqToEntitiesDomainService<QScheduleEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'AppointedPatients' query.
        public IQueryable<AppointedPatient> GetAppointedPatients()
        {
            return this.ObjectContext.AppointedPatients;
        }

        public AppointedPatient GetAppointedPatientsByHN(string hn)
        {
            return this.ObjectContext.AppointedPatients.SingleOrDefault(p => p.HN == hn);
        }

        public void InsertAppointedPatient(AppointedPatient appointedPatient)
        {
            if ((appointedPatient.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(appointedPatient, EntityState.Added);
            }
            else
            {
                this.ObjectContext.AppointedPatients.AddObject(appointedPatient);
            }
        }

        public void UpdateAppointedPatient(AppointedPatient currentAppointedPatient)
        {
            this.ObjectContext.AppointedPatients.AttachAsModified(currentAppointedPatient, this.ChangeSet.GetOriginal(currentAppointedPatient));
        }

        public void DeleteAppointedPatient(AppointedPatient appointedPatient)
        {
            if ((appointedPatient.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(appointedPatient, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.AppointedPatients.Attach(appointedPatient);
                this.ObjectContext.AppointedPatients.DeleteObject(appointedPatient);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Appointments' query.
        public IQueryable<Appointment> GetAppointments()
        {
            return this.ObjectContext.Appointments.Include("Service")
                                                  .Include("AppointedPatient")
                                                  .Include("AppointmentDetails.PreOrder")
                                                  .Include("Logs")
                                                  .Where(p => !p.IsDeleted);
        }

        public void InsertAppointment(Appointment appointment)
        {
            if ((appointment.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(appointment, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Appointments.AddObject(appointment);
            }
        }

        public void UpdateAppointment(Appointment currentAppointment)
        {
            this.ObjectContext.Appointments.AttachAsModified(currentAppointment, this.ChangeSet.GetOriginal(currentAppointment));
        }

        public void DeleteAppointment(Appointment appointment)
        {
            if ((appointment.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(appointment, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Appointments.Attach(appointment);
                this.ObjectContext.Appointments.DeleteObject(appointment);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'AppointmentDetails' query.
        public IQueryable<AppointmentDetail> GetAppointmentDetails()
        {
            return this.ObjectContext.AppointmentDetails;
        }

        public void InsertAppointmentDetail(AppointmentDetail appointmentDetail)
        {
            if ((appointmentDetail.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(appointmentDetail, EntityState.Added);
            }
            else
            {
                this.ObjectContext.AppointmentDetails.AddObject(appointmentDetail);
            }
        }

        public void UpdateAppointmentDetail(AppointmentDetail currentAppointmentDetail)
        {
            this.ObjectContext.AppointmentDetails.AttachAsModified(currentAppointmentDetail, this.ChangeSet.GetOriginal(currentAppointmentDetail));
        }

        public void DeleteAppointmentDetail(AppointmentDetail appointmentDetail)
        {
            if ((appointmentDetail.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(appointmentDetail, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.AppointmentDetails.Attach(appointmentDetail);
                this.ObjectContext.AppointmentDetails.DeleteObject(appointmentDetail);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Holidays' query.
        public IQueryable<Holiday> GetHolidays()
        {
            return this.ObjectContext.Holidays.Where(p => !p.IsDeleted) // Deleted
                                              .OrderBy(p => p.HolidayDate);
        }

        public void InsertHoliday(Holiday holiday)
        {
            if ((holiday.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(holiday, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Holidays.AddObject(holiday);
            }
        }

        public void UpdateHoliday(Holiday currentHoliday)
        {
            this.ObjectContext.Holidays.AttachAsModified(currentHoliday, this.ChangeSet.GetOriginal(currentHoliday));
        }

        public void DeleteHoliday(Holiday holiday)
        {
            if ((holiday.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(holiday, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Holidays.Attach(holiday);
                this.ObjectContext.Holidays.DeleteObject(holiday);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'LogAppointments' query.
        public IQueryable<LogAppointment> GetLogAppointments()
        {
            return this.ObjectContext.LogAppointments;
        }

        public void InsertLogAppointment(LogAppointment logAppointment)
        {
            if ((logAppointment.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(logAppointment, EntityState.Added);
            }
            else
            {
                this.ObjectContext.LogAppointments.AddObject(logAppointment);
            }
        }

        public void UpdateLogAppointment(LogAppointment currentLogAppointment)
        {
            this.ObjectContext.LogAppointments.AttachAsModified(currentLogAppointment, this.ChangeSet.GetOriginal(currentLogAppointment));
        }

        public void DeleteLogAppointment(LogAppointment logAppointment)
        {
            if ((logAppointment.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(logAppointment, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.LogAppointments.Attach(logAppointment);
                this.ObjectContext.LogAppointments.DeleteObject(logAppointment);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'LogHolidays' query.
        public IQueryable<LogHoliday> GetLogHolidays()
        {
            return this.ObjectContext.LogHolidays;
        }

        public void InsertLogHoliday(LogHoliday logHoliday)
        {
            if ((logHoliday.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(logHoliday, EntityState.Added);
            }
            else
            {
                this.ObjectContext.LogHolidays.AddObject(logHoliday);
            }
        }

        public void UpdateLogHoliday(LogHoliday currentLogHoliday)
        {
            this.ObjectContext.LogHolidays.AttachAsModified(currentLogHoliday, this.ChangeSet.GetOriginal(currentLogHoliday));
        }

        public void DeleteLogHoliday(LogHoliday logHoliday)
        {
            if ((logHoliday.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(logHoliday, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.LogHolidays.Attach(logHoliday);
                this.ObjectContext.LogHolidays.DeleteObject(logHoliday);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'LogPhysicianAbsents' query.
        public IQueryable<LogPhysicianAbsent> GetLogPhysicianAbsents()
        {
            return this.ObjectContext.LogPhysicianAbsents;
        }

        public void InsertLogPhysicianAbsent(LogPhysicianAbsent logPhysicianAbsent)
        {
            if ((logPhysicianAbsent.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(logPhysicianAbsent, EntityState.Added);
            }
            else
            {
                this.ObjectContext.LogPhysicianAbsents.AddObject(logPhysicianAbsent);
            }
        }

        public void UpdateLogPhysicianAbsent(LogPhysicianAbsent currentLogPhysicianAbsent)
        {
            this.ObjectContext.LogPhysicianAbsents.AttachAsModified(currentLogPhysicianAbsent, this.ChangeSet.GetOriginal(currentLogPhysicianAbsent));
        }

        public void DeleteLogPhysicianAbsent(LogPhysicianAbsent logPhysicianAbsent)
        {
            if ((logPhysicianAbsent.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(logPhysicianAbsent, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.LogPhysicianAbsents.Attach(logPhysicianAbsent);
                this.ObjectContext.LogPhysicianAbsents.DeleteObject(logPhysicianAbsent);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'LogPhysicianSchedules' query.
        public IQueryable<LogPhysicianSchedule> GetLogPhysicianSchedules()
        {
            return this.ObjectContext.LogPhysicianSchedules;
        }

        public void InsertLogPhysicianSchedule(LogPhysicianSchedule logPhysicianSchedule)
        {
            if ((logPhysicianSchedule.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(logPhysicianSchedule, EntityState.Added);
            }
            else
            {
                this.ObjectContext.LogPhysicianSchedules.AddObject(logPhysicianSchedule);
            }
        }

        public void UpdateLogPhysicianSchedule(LogPhysicianSchedule currentLogPhysicianSchedule)
        {
            this.ObjectContext.LogPhysicianSchedules.AttachAsModified(currentLogPhysicianSchedule, this.ChangeSet.GetOriginal(currentLogPhysicianSchedule));
        }

        public void DeleteLogPhysicianSchedule(LogPhysicianSchedule logPhysicianSchedule)
        {
            if ((logPhysicianSchedule.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(logPhysicianSchedule, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.LogPhysicianSchedules.Attach(logPhysicianSchedule);
                this.ObjectContext.LogPhysicianSchedules.DeleteObject(logPhysicianSchedule);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'PhysicianAbsents' query.
        public IQueryable<PhysicianAbsent> GetPhysicianAbsents()
        {
            return this.ObjectContext.PhysicianAbsents.Include("Service")
                                                      .Include("Logs")
                                                      .Where(p => !p.IsDeleted)
                                                      .OrderBy(p => p.BeginDate);
        }

        public IQueryable<PhysicianAbsent> GetPhysicianAbsentsByPhysicianCode(string physicianCode)
        {
            return this.ObjectContext.PhysicianAbsents.Include("Service")
                                                      .Include("Logs")
                                                      .Where(p => p.Physician == physicianCode)
                                                      .Where(p => !p.IsDeleted)
                                                      .OrderBy(p => p.BeginDate);
        }


        public void InsertPhysicianAbsent(PhysicianAbsent physicianAbsent)
        {
            if ((physicianAbsent.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(physicianAbsent, EntityState.Added);
            }
            else
            {
                this.ObjectContext.PhysicianAbsents.AddObject(physicianAbsent);
            }
        }

        public void UpdatePhysicianAbsent(PhysicianAbsent currentPhysicianAbsent)
        {
            this.ObjectContext.PhysicianAbsents.AttachAsModified(currentPhysicianAbsent, this.ChangeSet.GetOriginal(currentPhysicianAbsent));
        }

        public void DeletePhysicianAbsent(PhysicianAbsent physicianAbsent)
        {
            if ((physicianAbsent.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(physicianAbsent, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.PhysicianAbsents.Attach(physicianAbsent);
                this.ObjectContext.PhysicianAbsents.DeleteObject(physicianAbsent);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'PhysicianSchedules' query.
        public IQueryable<PhysicianSchedule> GetPhysicianSchedules()
        {
            return this.ObjectContext.PhysicianSchedules.Include("Service").Include("Logs")
                                                        .Where(p => !p.IsDeleted) // Deleted
                                                        .OrderBy(p => p.ServiceID)
                                                        .OrderBy(p => p.DayOfWeek);
        }

        public IQueryable<PhysicianSchedule> GetPhysicianSchedulesByPhysicianCode(string physicianCode)
        {
            return this.ObjectContext.PhysicianSchedules.Include("Service").Include("Logs")
                                                        .Where(p => !p.IsDeleted) // Deleted
                                                        .Where(p => p.Physician == physicianCode)
                                                        .OrderBy(p => p.ServiceID)
                                                        .OrderBy(p => p.DayOfWeek);
        }

        public void InsertPhysicianSchedule(PhysicianSchedule physicianSchedule)
        {
            if ((physicianSchedule.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(physicianSchedule, EntityState.Added);
            }
            else
            {
                this.ObjectContext.PhysicianSchedules.AddObject(physicianSchedule);
            }
        }

        public void UpdatePhysicianSchedule(PhysicianSchedule currentPhysicianSchedule)
        {
            this.ObjectContext.PhysicianSchedules.AttachAsModified(currentPhysicianSchedule, this.ChangeSet.GetOriginal(currentPhysicianSchedule));
        }

        public void DeletePhysicianSchedule(PhysicianSchedule physicianSchedule)
        {
            if ((physicianSchedule.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(physicianSchedule, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.PhysicianSchedules.Attach(physicianSchedule);
                this.ObjectContext.PhysicianSchedules.DeleteObject(physicianSchedule);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'PreOrders' query.
        public IQueryable<PreOrder> GetPreOrders()
        {
            return this.ObjectContext.PreOrders;
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Services' query.
        public IQueryable<Service> GetServices()
        {
            return this.ObjectContext.Services.OrderBy(p => p.ServiceID);
        }
    }
}


